﻿
using System.Collections;
using UnityEngine;

//要实现残影，创建一个挂载此脚本的预制件（一般来说只需要SpriteRenderer组件），然后交给对象池预生成
[RequireComponent(typeof(SpriteRenderer))]
public class Afterimage : MyEffect
{
    protected SpriteRenderer m_spriteRenderer;

    protected Transform sourceTransform;    //非必要      
    protected SpriteRenderer sourceSprite;  //非必要
    protected int frame;                    //存在时间（固定帧数）
    protected Color originColor;
    protected Color targetColor;
    protected Color _deltaColor;

    public override void OnCreate()
    {
        base.OnCreate();
        m_spriteRenderer = GetComponent<SpriteRenderer>();
        originColor = m_spriteRenderer.color;
        targetColor = originColor.ResetAlpha(0f);
        _deltaColor = (targetColor - originColor) / frame;
        frame = (int)(t_exist * 50);
    }

    protected override IEnumerator LifeCycle()
    {
        m_spriteRenderer.color = originColor;
        for (int i = 0; i < frame; i++)
        {
            m_spriteRenderer.color += _deltaColor;
            yield return new WaitForFixedUpdate();
        }
        Recycle();
    }
}
